package com.jt.aop;

import com.jt.service.DeptServiceImpl;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class SpringAOP {
    //定义before通知
   /* @Before("bean(deptServiceImpl)")*/
//    @Before("within(com.jt.*.DeptServiceImpl)")
//    @Before("execution(void com.jt.service.DeptServiceImpl.addDept())")
    @Pointcut("@annotation(com.jt.anno.Cache)")
    public void pointcut(){

    }
    @Before("pointcut()")
    public void before(JoinPoint joinPoint){
        System.out.println(joinPoint.getTarget().getClass());
        System.out.println(joinPoint.getSignature().getDeclaringType());
        System.out.println(joinPoint.getSignature().getName());
        System.out.println("我是before通知");
    }
    @AfterReturning(pointcut="pointcut()",returning = "ags")
    public void afterReturning(JoinPoint joinPoint,Object ags){
        System.out.println("我是AfterReturning通知");
    }
    @AfterThrowing(pointcut="pointcut()",throwing = "e")
    public void afterThrowing(JoinPoint joinPoint,Exception e){
        System.out.println(e.getMessage());
        System.out.println(e.getClass());
        System.out.println("我是异常通知");
    }
    @After("pointcut()")
    public void after(){
        System.out.println("我是after通知");
    }
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint point){
        Object result=null;
        try {
            System.out.println("环绕开始");
            long l = System.currentTimeMillis();
            result = point.proceed();
            System.out.println("环绕结束");
            long l1 = System.currentTimeMillis();
            System.out.println("耗时:"+(l1-l));
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return result;
    }


}
